<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2006
sirfragalot@users.sourceforge.net

**********************************************************************************/
/*****
* BROWSEKEYWORD class
*
* Browse keywords as a 'tag cloud'
*
* 
$Header: /cvsroot/wikindx/wikindx3/core/browse/BROWSEKEYWORD.php,v 1.10 2007/07/20 10:38:09 sirfragalot Exp $
*****/
class BROWSEKEYWORD
{
// Constructor
	function BROWSEKEYWORD($db, $vars)
	{
		$this->db = $db;
		$this->vars = $vars;
		include_once("core/html/MISC.php");
		include_once("core/html/HTML.php");
		$this->html = new HTML();
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
		include_once("core/browse/BROWSECOMMON.php");
		$this->common = new BROWSECOMMON($this->db);
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
	}
	
// display keywords
	function display()
	{
		$this->template->setVar('heading', $this->messages->text("heading", "browseKeyword"));
		$this->sum = $this->keyword = array();
		$this->getKeywords();
		if(empty($this->keyword))
		{
			$pString = MISC::p("&nbsp;") . $this->messages->text("misc", "noKeywords");
			$this->template->setVar('body', $pString);
			return $this->template->process();	
		}
		/*tri
		natcasesort($this->keyword);
		natcasesort($this->sum);*/
		asort($this->keyword);
		asort($this->sum);
		$this->keyword = $this->common->paging($this->vars, $this->keyword);
		$bib = $this->bib ? MISC::p($this->bib, "hint") : FALSE;
		$pString = $bib . $this->common->linksInfo() . 
			MISC::pBrowse($this->process()/*,"center"*/);
		$this->template->setVar('body', $pString);
		$this->template->setVar('paging', $this->common->pagingLinks("action=browseKeyword"));
		return $this->template->process();
	}
	
// display keywords for metadata
	function displayMeta()
	{
		$this->template->setVar('heading', $this->messages->text("heading", "browseKeyword"));
		$this->sum = $this->keyword = array();
		$this->getMetaKeywords();
		if(empty($this->keyword))
		{
			$pString = MISC::p("&nbsp;") . $this->messages->text("misc", "noKeywords");
			$this->template->setVar('body', $pString);
			return $this->template->process();
		}
		asort($this->sum);
		asort($this->keyword);
		$this->keyword = $this->common->paging($this->vars, $this->keyword);
		$bib = $this->bib ? MISC::p($this->bib, "hint") : FALSE;
		$pString = $bib . $this->common->linksInfo() . 
			MISC::pBrowse($this->process(TRUE));
		$this->template->setVar('body', $pString);
		$this->template->setVar('paging', $this->common->pagingLinks("action=browseMetaKeyword"));
		return $this->template->process();
	}
	
// Process and display results
	function process($meta = FALSE)
	{
		$lowestSum = current($this->sum);
		$highestSum = end($this->sum);
		foreach($this->keyword as $id => $name)
		{
			//$name = str_replace(' ', "&nbsp;", $name);
			$name = $this->html->codeToHtml($name);
			//$colour = $this->common->colourText($lowestSum, $highestSum, $this->sum[$id]);
			//$size = $this->common->sizeText($lowestSum, $highestSum, $this->sum[$id]);
			if(!$meta)
				$links[] = MISC::aBrowse($colour, $size, $name, "index.php?action=listKeywordProcess" . 
					htmlentities("&id=" . $id)) . "&nbsp;[" . $this->sum[$id] . "]";
			else
				$links[] = MISC::aBrowse($colour, $size, $name, "index.php?action=metaKeywordProcess" . 
					htmlentities("&id=" . $id)) . "&nbsp;[" . $this->sum[$id] . "]";
		}
		//return join("&nbsp;", $links);
		return join("<br>", $links);
	}
	
// Get keywords from db
	function getKeywords()
	{
		list($condition, $bib) = $this->common->userBibCondition('WKX_resource_keywords');
		//list($condition, $bib) = $this->common->userBibCondition('WKX_keyword');
		if($condition)
			$condition = " WHERE " . $condition;
		$recordset = $this->db->select(array('WKX_resource_keyword'), array('id', 'keywords'), $condition);
		//$recordset = $this->db->select(array('WKX_keyword'), array('id', 'keyword'), $condition);
		while($row = $this->db->loopRecordSet($recordset))
		{
			$this->collate($row, 'keywords');
		}
		if($bib)
			$this->bib = $this->messages->text("user", "bibliography") . ": " . $bib;
		else if($this->session->getVar('setup_multiUser'))
			$this->bib = $this->messages->text("user", "bibliography") . ": " . 
				$this->messages->text("user", "masterBib");
		else
			$this->bib = FALSE;
	}
	
// Get keywords for metadata from db
	function getMetaKeywords()
	{
		list($condition, $bib) = $this->common->userBibMetaCondition('WKX_resource_quote');
		if($condition)
			$condition = " WHERE " . $condition;
		$recordset = $this->db->select(array('WKX_resource_quote'), 
			array('id', 'quote_keywords'), $condition);
		while($row = $this->db->loopRecordSet($recordset))
			$this->collate($row, 'quote_keywords');
		$recordset = $this->db->select(array('WKX_resource_paraphrase'), 
			array('id', 'paraphrase_keywords'), $condition);
		while($row = $this->db->loopRecordSet($recordset))
			$this->collate($row, 'paraphrase_keywords');
		$recordset = $this->db->select(array('WKX_resource_musing'), 
			array('id', 'musing_keywords'), $condition);
		while($row = $this->db->loopRecordSet($recordset))
			$this->collate($row, 'musing_keywords');
		if($bib)
			$this->bib = $this->messages->text("user", "bibliography") . ": " . $bib;
		else if($this->session->getVar('setup_multiUser'))
			$this->bib = $this->messages->text("user", "bibliography") . ": " . 
				$this->messages->text("user", "masterBib");
		else
			$this->bib = FALSE;
	}
	
// Add keywords to array and sum totals
	function collate($row, $field)
	{
		/*foreach(split(",", $row[$field]) as $key => $value)
		{
			if($value)
				$keywords[] = "$value";
		}*/
		//print_r($row);
		$split = split(",",$row[$field]);
		foreach($split as $key => $value)
		{
			if($value) $keywords[] = "$value";
			//else echo $value;
		}
		if(!isset($keywords))
			return FALSE;
		foreach($keywords as $keywordId)
		{
			if(!array_key_exists($keywordId, $this->sum))
				$this->sum[$keywordId] = 1;
			else
				++$this->sum[$keywordId];
			$keywordsCondition[] = "'$keywordId'";
		}
		
		if(!isset($keywordsCondition))
			return;
		$keywordsCondition = array_unique($keywordsCondition);
		$condition = $this->db->formatField('id') . "=" . join(" OR " .
			$this->db->formatField('id') . "=", $keywordsCondition);
		$recordset = $this->db->select(array('WKX_keyword'), array("id", "keyword"), 
			" WHERE " . $condition);
		while($row = $this->db->loopRecordSet($recordset))
		{
			$this->keyword[$row['id']] = preg_replace("/{(.*)}/U", "$1", stripslashes($row['keyword']));
		}
	}
}
?>